/*
 *  Copyright (C) 2009 Dominik Psenner, Christoph Caks
 * 
 *   This file is part of project-blog.
 *
 *  project-blog is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  project-blog is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with project-blog.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.blog.markup.docbook;

import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.ErrorListener;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Logger;

import com.blog.markup.MarkupTransformer;

/**
 * performs an XSLT transformation by using the DocBook stylesheet.
 * 
 * @author Christoph Caks
 * 
 */
public class DocBookMarkupTransformer implements MarkupTransformer {
	private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger("project-blog.markup.docbook");
	
	private static final Logger log4j = Logger.getLogger(DocBookMarkupTransformer.class);
	
	@Override
	public String extractTitle(final String source) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public String transform(final String input) {
		String result;
		final StringWriter resultWriter = new StringWriter();
		try {
			final javax.xml.transform.TransformerFactory tFactory = javax.xml.transform.TransformerFactory.newInstance(
					"org.apache.xalan.processor.TransformerFactoryImpl", getClass().getClassLoader());
			tFactory.setErrorListener(new ErrorListener() {
				@Override
				public void error(final TransformerException exception) throws TransformerException {
					log.throwing(this.getClass().getName(), "transform", exception);
					log4j.error("LISTENER", exception);
				}
				
				@Override
				public void fatalError(final TransformerException exception) throws TransformerException {
					log.throwing(this.getClass().getName(), "transform", exception);
					log4j.fatal("LISTENER", exception);
				}
				
				@Override
				public void warning(final TransformerException exception) throws TransformerException {
					log.throwing(this.getClass().getName(), "transform", exception);
					log4j.warn("LISTENER", exception);
				}
			});
			final Source xmlSource = new StreamSource(new StringReader(input));
			log4j.debug("xmlSource = " + xmlSource);
			final Source xslSource = new StreamSource(getClass().getResourceAsStream("/docbook-xsl-1.75.2/html/docbook.xsl"));
			log4j.debug("xslSource = " + xslSource);
			log4j.debug("xslSource.getInputStream() = " + ((StreamSource) xslSource).getInputStream());
			final Result htmlResult = new StreamResult(resultWriter);
			final Transformer transformer = tFactory.newTransformer(xslSource);
			transformer.transform(xmlSource, htmlResult);
			result = resultWriter.toString();
		}
		catch (final Exception e) {
			log4j.error("error while transform", e);
			e.printStackTrace(new PrintWriter(resultWriter));
			
			result = resultWriter.toString();
		}
		return result;
	}
	
}
